#include "StdAfx.h"
#include "ImportIDSTool.h"
#include "comutil.h"

CImportIDSTool::CImportIDSTool(void)
{
}


CImportIDSTool::~CImportIDSTool(void)
{
}

bool CImportIDSTool::Start()
{
	return true;
}

bool CImportIDSTool::End()
{
	return true;
}

CSystem * CImportIDSTool::Import(const CString & strPath)
{
	IXMLDOMDocument *pDoc=NULL;
	HRESULT hr=::CoCreateInstance(CLSID_DOMDocument, NULL, CLSCTX_INPROC_SERVER, IID_IXMLDOMDocument, (void **)&pDoc);
	if(hr!=S_OK)
		return NULL;

	VARIANT_BOOL isSuccessful;
	hr=pDoc->load(_variant_t(strPath), &isSuccessful);
	if(hr!=S_OK || isSuccessful!=VARIANT_TRUE)
	{
		pDoc->Release();

		return NULL;
	}

	pDoc->Release();

	return NULL;
}

IXMLDOMNode * CImportIDSTool::GetChildNodeByName(IXMLDOMNode *pRoot, const CString & strName)
{
	ASSERT(pRoot);
	IXMLDOMNodeList *pChildList=NULL;
	HRESULT hr=pRoot->get_childNodes(&pChildList);
	if(hr==S_OK)
	{
		long lNum=0;
		pChildList->get_length(&lNum);
		for(long i=0; i<lNum; i++)
		{
			IXMLDOMNode *pChild=NULL;
			hr=pChildList->get_item(i, &pChild);
			if(hr==S_OK)
			{
				BSTR bstrNodeName;
				pChild->get_nodeName(&bstrNodeName);
				CString strNodeName(bstrNodeName);
				if(!strNodeName.CompareNoCase(strName))
					return pChild;
				pChild->Release();
			}
		}
		pChildList->Release();
	}

	return NULL;
}

bool CImportIDSTool::GetAttribValueByName(IXMLDOMNode *pRoot, const CString & strName, double & dblValue)
{
	bool bFind=false;
	IXMLDOMNamedNodeMap *pAttribMap=NULL;
	HRESULT hr=pRoot->get_attributes(&pAttribMap);
	if(hr==S_OK)
	{
		long lAttribNum=0;
		pAttribMap->get_length(&lAttribNum);
		for(long i=0; i<lAttribNum; i++)
		{
			IXMLDOMNode *pNode=NULL;
			hr=pAttribMap->get_item(i, &pNode);
			if(hr==S_OK)
			{
				IXMLDOMAttribute *pAttrib=NULL;
				hr=pNode->QueryInterface(IID_IXMLDOMAttribute, (void **)&pAttrib);
				if(hr==S_OK)
				{
					BSTR bstrAttribName;
					pAttrib->get_name(&bstrAttribName);
					CString strAttribName(bstrAttribName);
					if(!strAttribName.CompareNoCase(strName))
					{
						_variant_t vValue;
						pAttrib->get_value(&vValue);
						dblValue=(double)vValue;
						bFind=true;
						break;
					}
					pAttrib->Release();
				}
				pNode->Release();
			}
		}
		pAttribMap->Release();
	}

	return bFind;
}

bool CImportIDSTool::GetAttribValueByName(IXMLDOMNode *pRoot, const CString & strName, _variant_t & vValue)
{
	bool bFind=false;
	//try
	//{
	IXMLDOMNamedNodeMap *pAttribMap=NULL;
	HRESULT hr=pRoot->get_attributes(&pAttribMap);
	if(hr==S_OK)
	{
		long lAttribNum=0;
		pAttribMap->get_length(&lAttribNum);
		for(long i=0; i<lAttribNum; i++)
		{
			IXMLDOMNode *pNode=NULL;
			hr=pAttribMap->get_item(i, &pNode);
			if(hr==S_OK)
			{
				IXMLDOMAttribute *pAttrib=NULL;
				hr=pNode->QueryInterface(IID_IXMLDOMAttribute, (void **)&pAttrib);
				if(hr==S_OK)
				{
					BSTR bstrAttribName;
					pAttrib->get_name(&bstrAttribName);
					CString strAttribName(bstrAttribName);
					if(!strAttribName.CompareNoCase(strName))
					{
						pAttrib->get_value(&vValue);
						bFind=true;
						break;
					}
					pAttrib->Release();
				}
				pNode->Release();
			}
		}
		pAttribMap->Release();
	}
	//}
	//catch(...)
	//{
	//	return false;
	//}

	return bFind;
}
